package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.Comment;
import com.itheima.pojo.User;
import com.itheima.service.CommentService;
import com.itheima.service.UserService;
import com.itheima.utils.CookieUtils;
import com.itheima.utils.Result;
import com.itheima.utils.StatusCode;
import org.elasticsearch.http.netty4.pipelining.HttpPipelinedRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/web/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    @Autowired
    private UserService userService;

    /**
     * 增加评论
     *
     * @Param Comment
     */
    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        Integer count = commentService.add(comment);
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "插入成功");
        } else {
            return new Result<>(StatusCode.FAIL, "插入失败");
        }
    }

    /**
     * 删除评论
     *
     * @Param Comment
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Comment comment) {
        Integer count = commentService.delete(comment);
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "删除成功");
        } else {
            return new Result<>(StatusCode.FAIL, "删除失败");
        }
    }

    /**
     * 获取评论列表
     *
     * @Param Comment
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Comment comment) {
        Page<Comment> comments = commentService.getList(comment);

        Map<String, Object> map = new HashMap<>();
        map.put("records", comments.getResult());
        map.put("total", comments.getTotal());
        map.put("size", comments.getPageSize());
        map.put("current", comments.getPageNum());
        map.put("orders", null);
        map.put("optimizeCountSql", true);
        map.put("isSearchCount", true);

        return new Result<>(StatusCode.SUCCESS, map);
    }

    /**
     * 关闭评论邮件通知
     * @param userUid
     * @return
     */

    @GetMapping("/closeEmailNotification/{userUid}")
    public Result closeEmailNotification(@PathVariable String userUid) {
        commentService.closeEmailNotification(userUid);
        return new Result<>(StatusCode.SUCCESS, "关闭成功");

    }

    /**
     * 获取评论列表和回复
     * @param user
     * @return
     */
    @PostMapping("/getListByUser")
    public Result getListByUser(@RequestBody User user) {



        Comment comment=new Comment();
        comment.setCurrentPage(user.getCurrentPage());
        comment.setPageSize(user.getPageSize());
        comment.setUserUid(user.getUid());
        comment.setToUserUid(user.getUid());
        Page<Comment> comments = commentService.getList(comment);


        Map<String, Object> map = new HashMap<>();
        map.put("records", comments.getResult());
        map.put("total", comments.getTotal());
        map.put("size", comments.getPageSize());
        map.put("current", comments.getPageNum());
        map.put("orders", null);
        map.put("optimizeCountSql", true);
        map.put("isSearchCount", true);

        return new Result<>(StatusCode.SUCCESS, map);
    }

    /**
     * 获取用户点赞信息
     * @param request
     * @param currentPage
     * @param pageSize
     * @return
     */

    @PostMapping("/getPraiseListByUser")
    public Result getPraiseListByUser(HttpServletRequest request,@RequestBody int currentPage,@RequestBody int pageSize) {

        String username = CookieUtils.getCookieValue(request, CookieUtils.LOGIN_USER, true);
        User user=userService.findByUsername(username);
        Comment comment=new Comment();
        comment.setCurrentPage(currentPage);
        comment.setPageSize(pageSize);
        comment.setUserUid(user.getUid());
        comment.setType(1);
        Page<Comment> comments = commentService.getList(comment);


        Map<String, Object> map = new HashMap<>();
        map.put("records", comments.getResult());
        map.put("total", comments.getTotal());
        map.put("size", comments.getPageSize());
        map.put("current", comments.getPageNum());
        map.put("orders", null);
        map.put("optimizeCountSql", true);
        map.put("isSearchCount", true);

        return new Result<>(StatusCode.SUCCESS, map);

    }

    /**
     * 举报
     * @param comment
     * @param request
     * @return
     */

    @PostMapping("/report")
    public Result report(@RequestBody Comment comment , HttpServletRequest request,@RequestParam("content") String content) {

        String username = CookieUtils.getCookieValue(request, CookieUtils.LOGIN_USER, true);

       Integer count=commentService.report(comment,username,content);
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "举报成功");
        } else {
            return new Result<>(StatusCode.FAIL, "举报失败");
        }

    }
}
